package com.guigu.dh.api.service.impl;

import com.guigu.dh.common.result.Result;
import com.guigu.dh.pojo.hosp.Hospital;
import com.guigu.dh.repository.HospitalRepository;
import com.guigu.dh.api.service.ApiHospitalService;
import com.guigu.dh.vo.hosp.CheckSignKeyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApiHospitalServiceImpl implements ApiHospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;


    // 保存数据的方法
    @Override
    public Boolean saveHospital(Hospital hospital) {
        // 验证是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital byHoscode = hospitalRepository.getHospitalByHoscode(hoscode);

        // 如果存在则做修改，不存在则做添加
        if (byHoscode != null) {
            hospital.setId(byHoscode.getId());
            hospital.setStatus(byHoscode.getStatus());
            hospital.setCreateTime(byHoscode.getCreateTime());
        } else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
        }
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);

        Hospital save = hospitalRepository.save(hospital);
        return save != null;
    }

    // 查询的方法
    @Override
    public Result show(CheckSignKeyVo checkSignKeyVo) {
        Hospital byHoscode = hospitalRepository.getHospitalByHoscode(checkSignKeyVo.getHoscode());
        if (byHoscode != null && byHoscode.getSignKey().equals(checkSignKeyVo.getSignKey())){
            return Result.ok(byHoscode);
        }
        return Result.fail();
    }

}
